package com.yifan.arrayList;

import com.yifan.arrayList.inter.List;

/**
 * 顺序表
 * @author ：zhanglei
 * @date ：Created in 2020-09-09 15:53
 * @description：自定义ArrayList
 */
public class ArrayList implements List {

    private Object [] elementData; //底层数组来存储多个元素

    private int size;//存储的元素的个数，线性表的长度，注意，不是数组的长度

    public ArrayList() {
        this(10);
    }

    public ArrayList(int initSize) {
        if(initSize<0){
            throw new RuntimeException("error,init length<0："+initSize);
        }

        elementData = new Object[initSize];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Object get(int i) {
        if(i >= size){
            throw new IndexOutOfBoundsException("error,i >=size ："+i);
        }
        return elementData[i];
    }

    @Override
    public boolean isEmpty() {
        return 0 == size;
    }

    @Override
    public boolean contains(Object e) {
        /*for(int i =0;i<size;i++){
			if(e.equals(elementData[i])){
				return true;
			}
		}
		return false;*/
        return indexOf(e) > -1;
    }

    @Override
    public int indexOf(Object e) {
        for(int i =0;i<size;i++){
            if(e.equals(elementData[i])){
                return i;
            }
        }
        return -1;
    }

    @Override
    public void add(int i, Object e) {
        if(elementData.length == size){
            throw new RuntimeException("list full");
        }
        if(i < 0 || i>=size){
            throw new IndexOutOfBoundsException("error,i >=size ："+i);
        }
        //需要移动元素数据,将元素后移
        for(int j=size;i<j;j--){
            elementData[j] = elementData[j-1];
        }
        elementData[i] = e;
        size++;
    }

    @Override
    public void add(Object e) {
        if(elementData.length == size){
            throw new RuntimeException("list full");
        }
        elementData[size] = e;
        size++;

    }

    @Override
    public boolean addBefore(Object obj, Object e) {
        if(elementData.length == size){
            throw new RuntimeException("addBefore list full");
        }
        int i = indexOf(obj);
        add(i-1,e);
        return true;
    }

    @Override
    public boolean addAfter(Object obj, Object e) {
        if(elementData.length == size){
            throw new RuntimeException("addAfter list full");
        }
        int i = indexOf(obj);
        if(i == -1){
            throw new RuntimeException("not find obj "+obj);
        }
        add(i-1,e);
        return true;
    }

    @Override
    public Object remove(int i) {
        if(i < 0 || i>=size){
            throw new IndexOutOfBoundsException("error,i >=size ："+i);
        }
        Object temp = elementData[i];
        //需要移动元素数据,将元素后移
        for(int j=i;i<size-1;i++){
            elementData[i] = elementData[i+1];
        }
        elementData[size-1] = null;
        size--;
        return temp;
    }

    @Override
    public boolean remove(Object e) {
        if(contains(e)){
            int i = indexOf(e);
            remove(i);
            return true;
        }
        return false;
    }

    @Override
    public Object replace(int i, Object e) {
        if(i < 0 || i>=size){
            throw new IndexOutOfBoundsException("error,i >=size ："+i);
        }
        return elementData[i] = e;
    }

    @Override
    public String toString() {
        if(this.size ==0){
            return "[ ]";
        }
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        for(int i =0;i<size;i++){
            if(i != size -1){
                builder.append( this.get(i)+",");
            }else{
                builder.append( this.get(i));
            }
        }
        builder.append("]");
        return builder.toString();
    }

}
